package cn.com.truelore.base;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import cn.com.truelore.common.Constants;
import cn.com.truelore.service.GfoWebService;

import android.app.Activity;
import android.app.TabActivity;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Display;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class BaseTabActivity extends TabActivity implements OnTouchListener,
		OnGestureListener {

	private int _layoutResId;

	public GestureDetector mGestureDetector;
	public static final int SWIPE_MIN_DISTANCE = 120;
	public static final int SWIPE_MAX_OFF_PATH = 250;
	public static final int SWIPE_THRESHOLD_VELOCITY = 200;
	private LayoutInflater layoutInflater;

	public BaseTabActivity() {

	}

	public BaseTabActivity(int layoutResId) {
		_layoutResId = layoutResId;
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//
		setContentView(_layoutResId);
		mGestureDetector = new GestureDetector(this);
		// 
		ApplicationManager.getInstance().addActivity(this);
		initPageData();
		initButtonClick();
		
	}

	public void initButtonClick() {

	}

	public void initPageData() {
		layoutInflater = LayoutInflater.from(this.getApplicationContext());
	}

	public void setSharedPreferences(String name, String key, String value) {
		SharedPreferences.Editor sharedata = getSharedPreferences(name, 0)
				.edit();
		sharedata.putString(key, value);
		sharedata.commit();
	}

	public void setSharedPreferences(String name, Map<String, String> map) {
		SharedPreferences.Editor sharedata = getSharedPreferences(name, 0)
				.edit();
		Set<Map.Entry<String, String>> set = map.entrySet();
		Map.Entry<String, String> entry;
		String key = "";
		String value = "";
		for (Iterator<Map.Entry<String, String>> it = set.iterator(); it
				.hasNext();) {
			entry = (Map.Entry<String, String>) it.next();
			key = entry.getKey();
			value = entry.getValue();
			sharedata.putString(key, value);
		}
		sharedata.commit();
	}

	public String getSharedPreferences(String name, String key) {
		SharedPreferences sharedata = getSharedPreferences(name, 0);
		return sharedata.getString(key, "");
	}

	public boolean onDown(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
			float arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onLongPress(MotionEvent arg0) {
		// TODO Auto-generated method stub

	}

	public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
			float arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onShowPress(MotionEvent arg0) {
		// TODO Auto-generated method stub

	}

	public boolean onSingleTapUp(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean onTouch(View arg0, MotionEvent arg1) {
		onTouchEvent(arg1);
		return mGestureDetector.onTouchEvent(arg1);
	}
	

	public LayoutInflater getLayoutInflater() {
		return layoutInflater;
	}
	
	public void exitApplication()
	{
		ApplicationManager.getInstance().exit();
	}
}
